<?php
/*
 * Author : DucLH
 * Des : Automatic gen Repository
 */ 
error_reporting(E_ALL);
ini_set("display_errors", 1);
//Read All File In Entity
function getAllFileInFolder(){
	$sPath = __DIR__ . "/src/Pocker/PockerBundle/Entity";
	$aFiles = scandir($sPath, 1);
	//var_dump(count($aFiles));
	
	if(count($aFiles)){
		for ($i = 0; $i < count($aFiles); $i++) {
			if(strlen($aFiles[$i]) > 3 && substr($aFiles[$i], -1) == 'p'){
				//var_dump($aFiles[$i]);	
				readMyfile($sPath."/".$aFiles[$i]);
			}
		}
	}
	
}
//Read content file and replace
function readMyfile($sPath){
	//var_dump($sPath);
	//die();
	$sFileContent = file_get_contents($sPath, true);
	$convert = explode("\n", $sFileContent); //create array separate by new line
	$sNameTable ="";
	for ($i=0;$i<count($convert);$i++) 
	{
		$mystring = $convert[$i];
		$findme   = '@ORM\Table(name="';
		$pos = strpos($mystring, $findme);
		if (!($pos === false)) {
			$mystring = $convert[$i-2]; 
			$aTemp = explode("*", $mystring);
			$sNameTable = trim($aTemp[1]);
		}
		if($sNameTable != ""){
			break;
		}
	}
	$sTempReplace = '* @ORM\Entity(repositoryClass="Pocker\PockerBundle\Repository\\'  .$sNameTable . 'Repository")';
	//var_dump($sTempReplace);
	$sFileContent = str_replace("* @ORM\Entity", $sTempReplace, $sFileContent);
	//Write File
	file_put_contents($sPath, $sFileContent);
}
//
getAllFileInFolder();
?>